/**
 * 文  件  名：  ScenicSpotDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-14
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IScenicSpotDao;
import com.qiyou.entity.ScenicSpot;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 景点信息数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class ScenicSpotDaoImpl implements IScenicSpotDao {
    private Connection conn = null;

    public ScenicSpotDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(ScenicSpot scenicSpot) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_scenic_spots`(`scsName`, `scsType`, `scsCountry`, `scsProvince`, `scsCity`, `scsNums`,`scsPath`,`location`,`longitude`,`atitude`) values(?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
        Object[] params = TransformUtil.beanTOArray(scenicSpot, true);
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(ScenicSpot scenicSpot) throws SQLException {
        boolean result = false;
        String sql = "update `tb_scenic_spots` set `scsID` = ?, `scsName` = ?, `scsType` = ?, `scsCountry` = ?, `scsProvince` = ?, `scsCity` = ?, `scsNums` = ?, `scsPath`=?, `location`=?, `longitude`=?, `atitude`=? where `scsID` = ?";
        Object[] params = TransformUtil.beanTOArray(scenicSpot, false);
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doDelete(long scenicSpotID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_scenic_spots` where `scsID` = ?";
        result = DBUtil.execute(conn, sql, scenicSpotID);
        return result;
    }

    @Override
    public ScenicSpot getScenicSpot(long scenicSpotID) throws SQLException {
        ScenicSpot result = null;
        String sql = "select from `tb_scenic_spots` where `scsID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, scenicSpotID);
        if (null != all && all.size() > 0) {
            result = (ScenicSpot) TransformUtil.arrayToBean(all.get(0),
                    ScenicSpot.class);
        }
        return result;
    }

    @Override
    public List<ScenicSpot> getScenicSpots(Object... params)
            throws SQLException {
        List<ScenicSpot> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_scenic_spots` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            result = new ArrayList<ScenicSpot>(all.size());

            for (Object[] objects : all) {
                ScenicSpot scenicSpot = (ScenicSpot) TransformUtil.arrayToBean(
                        objects, ScenicSpot.class);
                result.add(scenicSpot);
            }
        }

        return result;
    }

}
